package ptu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import ptu.entity.Manager;
import ptu.entity.User;
import ptu.result.Result;
import ptu.service.UserService;

@RequestMapping("/user")
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 全部用户
     */
    @GetMapping("/selectAll")
    @ResponseBody
    public Result selectAll(){
        return userService.selectAll();
    }

    @GetMapping("/{id}")
    public String getUserById(@PathVariable Integer id,Model model) {
        User user = userService.getUserById(id);
        model.addAttribute("user", user);
        return "manager/user_update";
    }

    @GetMapping("/delete/{username}")
    @ResponseBody
    public Result delete(@PathVariable("username") String username) {
        return userService.delete(username);
    }

    /**
     * 登录
     * @param username
     * @param password
     * @return {@link String }
     */
    @PostMapping("/login")
    @ResponseBody
    public Result login(@RequestParam("username") String username,@RequestParam("password") String password){
        return  userService.login(username,password);
    }

    /**
     * 注册
     * @param user
     * @return {@link String }
     */
    @PostMapping("/register")
    @ResponseBody
    public Result register(User user){
        return  userService.register(user);
    }

    /**
     * 退出
     * @param username
     * @return {@link String }
     */
    @DeleteMapping("/logout/{username}")
    public String logout(@PathVariable String username){
        Result result = userService.logout(username);
        if("400".equals(result.getCode())){
            return "false";
        }
        return "true";
    }

    /**
     * 更新用户信息
     * @param user
     * @return {@link String }
     */
    @PostMapping("/update")
    @ResponseBody
    public Result update(User user){
        return userService.update(user);
    }

    /**
     * 确认某个用户是否存在
     * @param username
     * @return {@link String }
     */
    @GetMapping("/selectSignal/{username}")
    public String selectSignal(@PathVariable String username){
        Result result = userService.selectByUsername(username);
        if("用户不存在".equals(result.getMsg())){
            return "false";
        }
        return "true";
    }




}
